package com.dhl.cloud.course.service;


import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

import com.dhl.cloud.course.entity.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dhl.cloud.common.util.DateUtils;
import com.dhl.cloud.common.vdo.CourseBean;
import com.dhl.cloud.common.vdo.LessionBean;
import com.dhl.cloud.course.dao.LessionMapper;
import com.dhl.cloud.course.entity.Lession;

@Service
public class LessionService {
	@Autowired
	private LessionMapper lessionMapper;

	public Lession selectByPrimaryKey(Integer id) {
		return lessionMapper.selectByPrimaryKey(id);
	}

	//获取当天的课程lession
	public List<CourseBean> selectNowLession() {
		List<CourseBean> resultList = new ArrayList<CourseBean>();
		List<LessionBean> list = lessionMapper.selectNowLession();
		for (LessionBean lbb:list)
		{

			CourseBean bean = isexist(resultList,lbb.getCourseid());
			if (bean != null)
			{
				List<LessionBean> lb = bean.getLessionBean();

				LessionBean one = new LessionBean();
				one.setId(lbb.getId());
				one.setName(lbb.getName());
				one.setStarttime(lbb.getStarttime());
				one.setCourseid(lbb.getCourseid());

				lb.add(one);
			}
			else
			{
				CourseBean c = new CourseBean();
				c.setName(lbb.getCoursename());
				c.setId(lbb.getCourseid());
				c.setUsername(lbb.getUsername());
				List<LessionBean> lb = new ArrayList<LessionBean>();
				LessionBean one = new LessionBean();
				one.setId(lbb.getId());
				one.setName(lbb.getName());
				one.setStarttime(lbb.getStarttime());
				one.setCourseid(lbb.getCourseid());

				lb.add(one);

				c.setLessionBean(lb);

				resultList.add(c);
			}
		}
		return resultList;
	}

	private CourseBean isexist(List<CourseBean> list,Integer id)
	{
		for (CourseBean bean : list)
		{
			if (bean.getId() == id)
			{
				return bean;
			}
		}
		return null;
	}
	public List<Lession> selectBycourseid(Integer courseid) {
		return lessionMapper.selectBycourseid(courseid);
	}

	public int deleteByPrimaryKey(Integer id) {
		return lessionMapper.deleteByPrimaryKey(id);
	}

	public int insert(Lession c) {
		if (c.getId() != null)
		{
			Lession cc = lessionMapper.selectByPrimaryKey(c.getId());
			cc.setName(c.getName());
			String time = c.getStarttime();//regTime(c.getStarttime());
			cc.setStarttime(time);
			cc.setModifytime(DateUtils.getDateTime());
			return lessionMapper.updateByPrimaryKey(cc);
		}
		else
		{
			String time = c.getStarttime();//regTime(c.getStarttime());
			c.setStarttime(time);
			c.setCreatetime(DateUtils.getDateTime());
			return lessionMapper.insert(c);
		}

	}

//	private String regTime(String dateTimeString)
//	{
//        DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
//        ZonedDateTime dateTime = ZonedDateTime.parse(dateTimeString, formatter);
//		String regularTime = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//		return regularTime;
//	}
}
